using Generics in C# [closed]

Posted by Uphaar Goyal on Programmers See other posts from Programmers or by Uphaar Goyal
Published on 2011-03-13T18:58:08Z Indexed on 2011/03/14 0:17 UTC
Read the original article Hit count: 758

Filed under:
|

I have started looking into using generics in C#. As an example what i have done is that I have an abstract class which implements generic methods. these generic methods take a sql query, a connection string and the Type T as parameters and then construct the data set, populate the object and return it back. This way each business object does not need to have a method to populate it with data or construct its data set. All we need to do is pass the type, the sql query and the connection string and these methods do the rest.I am providing the code sample here. I am just looking to discuss with people who might have a better solution to what i have done.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MWTWorkUnitMgmtLib.Business;
using System.Collections.ObjectModel;
using System.Reflection;

    namespace MWTWorkUnitMgmtLib.TableGateway
    {
    public abstract class TableGateway
    {
        public TableGateway()
        {

        }

        protected abstract string GetConnection();
        protected abstract string GetTableName();

        public DataSet GetDataSetFromSql(string connectionString, string sql)
        {
            DataSet ds = null;
            using (SqlConnection connection = new SqlConnection(connectionString))
            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText = sql;
                connection.Open();
                using (ds = new DataSet())
                using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                {
                    adapter.Fill(ds);
                }
            }
            return ds;
        }

        public static bool ContainsColumnName(DataRow dr, string columnName)
        {
            return dr.Table.Columns.Contains(columnName); 
        }

        public DataTable GetDataTable(string connString, string sql)
        {
            DataSet ds = GetDataSetFromSql(connString, sql);
            DataTable dt = null;
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    dt = ds.Tables[0];
                }
            }
            return dt; 
        }

        public T Construct(DataRow dr, T t) where T : class, new()
        {
            Type t1 = t.GetType();
            PropertyInfo[] properties = t1.GetProperties();

            foreach (PropertyInfo property in properties)
            {
                if (ContainsColumnName(dr, property.Name) && (dr[property.Name] != null))
                    property.SetValue(t, dr[property.Name], null); 
            }

            return t; 
        }

        public T GetByID(string connString, string sql, T t) where T : class, new()
        {
            DataTable dt = GetDataTable(connString, sql);
            DataRow dr = dt.Rows[0];
            return Construct(dr, t); 
        }

        public List GetAll(string connString, string sql, T t) where T : class, new()
        {
            List collection = new List();
            DataTable dt = GetDataTable(connString, sql);
            foreach (DataRow dr in dt.Rows)
                collection.Add(Construct(dr, t));

            return collection; 
        }
    }
}

© Programmers or respective owner

Related posts about c#

Related posts about generics